package com.hbnu.controller;

import com.hbnu.pojo.DormBuild;
import com.hbnu.service.DormManagerService;
import com.hbnu.service.impl.DormManagerServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/dormBuild.action")
public class DormBuildServlet extends HttpServlet {

    DormManagerService dormManagerService = new DormManagerServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("utf-8");

        String action = req.getParameter("action");
        String id = req.getParameter("id");

        List<DormBuild> buildSelects = dormManagerService.find();
        req.setAttribute("buildSelects", buildSelects);

        if (action != null && "list".equals(action)) {
            List<DormBuild> builds = new ArrayList<>();
            if (id == null || "".equals(id)) {
                builds = dormManagerService.find();
            } else if (id != null && !"".equals(id)) {
                DormBuild build = dormManagerService.findById(Integer.parseInt(id));
                builds.add(build);
            }

            req.setAttribute("id", id);
            req.setAttribute("builds", builds);
            req.setAttribute("mainRight", "/jsp/dormBuildList.jsp");
            req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);
        } else if (action != null && "preAdd".equals(action)) {
            req.setAttribute("mainRight", "/jsp/dormBuildAddOrUpdate.jsp");
            req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);
        } else if (action != null & action.equals("save")) {
            // 保存数据
            String name = req.getParameter("name");
            String remark = req.getParameter("remark");
// 宿舍楼名字不能重复，从数据库查询，当前用户输入的宿舍楼名字是否已经存在
            DormBuild dormBuild = dormManagerService.findByName(name);
            if (id != null && !id.equals("")) {
// 更新
                if (dormBuild != null &&
                        !dormBuild.getId().equals(Integer.parseInt(id))) {
// 表示跳转到宿舍楼添加页面
                    req.setAttribute("error", "当前宿舍楼名已存在，请重新输入！");
// 根据宿舍楼id，查询宿舍楼
                    DormBuild build =
                            dormManagerService.findById(Integer.parseInt(id));
// 保存宿舍楼信息，到前端页面展示
                    req.setAttribute("build", build);
                    req.setAttribute("mainRight",
                            "/jsp/dormBuildAddOrUpdate.jsp");
                    req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);
                } else {
                    dormBuild = dormManagerService.findById(Integer.parseInt(id));
                    dormBuild.setName(name);
                    dormBuild.setRemark(remark);

                    // 执行更新
                    dormManagerService.update(dormBuild);
// 更新完成，跑到宿舍楼管理列表页，查询所有宿舍楼
                    List<DormBuild> builds = dormManagerService.find();
                    req.setAttribute("builds", builds);
                    req.setAttribute("mainRight", "/jsp/dormBuildList.jsp");
                    req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);
                }
            } else {
// 保存
                if (dormBuild != null) {
// 当前用户输入的宿舍楼名已存在
// 表示跳转到宿舍楼添加页面
                    req.setAttribute("error", "当前宿舍楼名已存在，请重新输入！");
                    req.setAttribute("mainRight",
                            "/jsp/dormBuildAddOrUpdate.jsp");
                    req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);
                } else {
// 当前用户输入的宿舍楼名不存在，则保存用户输入的信息到数据库
                    DormBuild build = new DormBuild();
                    build.setName(name);
                    build.setRemark(remark);
                    build.setDisabled(0);
                    dormManagerService.save(build);
// 保存完成，跑到宿舍楼管理列表页，查询所有宿舍楼
                    List<DormBuild> builds = dormManagerService.find();
                    req.setAttribute("builds", builds);
                    req.setAttribute("mainRight", "/jsp/dormBuildList.jsp");
                    req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);
                }
            }
        } else if (action != null & action.equals("preUpdate")) {
// 根据宿舍楼id，查询宿舍楼
            DormBuild build = dormManagerService.findById(Integer.parseInt(id));
// 保存宿舍楼信息，到前端页面展示
            req.setAttribute("build", build);
// 跳转到宿舍楼修改页面
            req.setAttribute("mainRight", "/jsp/dormBuildAddOrUpdate.jsp");
            req.getRequestDispatcher("/jsp/main.jsp").forward(req, resp);


        } else if (action != null & action.equals("deleteOrActive")) {
// 删除或激活
            String disabled = req.getParameter("disabled");
            DormBuild dormBuild = dormManagerService.findById(Integer.parseInt(id));
            dormBuild.setDisabled(Integer.parseInt(disabled));
// 执行更新
            dormManagerService.update(dormBuild);
// 更新完成，跑到宿舍楼管理列表页，查询所有宿舍楼
            List<DormBuild> builds = dormManagerService.find();
            req.setAttribute("builds", builds);
            req.setAttribute("mainRight", "/jsp/dormBuildList.jsp");
            req.getRequestDispatcher("/jsp/main.jsp").forward(req,
                    resp);
        }


    }
}